﻿
Imports System.Runtime.CompilerServices
Imports osi.root.connector
Imports osi.root.delegates
Imports osi.root.lock

'according to generic_perf test,
'the performance to directly access the p should be on-par with other kind of wrapper logic
Public Class wrapper(Of T As Structure)
    Public p As T
End Class

Public Module _extension
    'formation refers lock
    <Extension()> Public Function in_use(ByVal i As wrapper(Of singleentry)) As Boolean
        If i Is Nothing Then
            Return False
        Else
            Return i.p.in_use()
        End If
    End Function

    <Extension()> Public Function not_in_use(ByVal i As wrapper(Of singleentry)) As Boolean
        If i Is Nothing Then
            Return False
        Else
            Return i.p.not_in_use()
        End If
    End Function

    <Extension()> Public Function mark_in_use(ByVal i As wrapper(Of singleentry)) As Boolean
        If i Is Nothing Then
            Return False
        Else
            Return i.p.mark_in_use()
        End If
    End Function

    <Extension()> Public Sub release(ByVal i As wrapper(Of singleentry))
        If Not i Is Nothing Then
            i.p.release()
        End If
    End Sub

    <Extension()> Public Sub mark_not_in_use(ByVal i As wrapper(Of singleentry))
        If Not i Is Nothing Then
            i.p.mark_not_in_use()
        End If
    End Sub

    <Extension()> Public Function increment(ByVal this As wrapper(Of unchecked_int),
                                            Optional ByVal that As Int32 = 1) As Int32
        this.p += that
        Return this.p
    End Function
End Module
